import org.apache.lucene.analysis.standard.StandardAnalyzer
import org.apache.lucene.analysis.th.ThaiAnalyzer
import org.apache.lucene.analysis.StopFilter;
import org.apache.lucene.analysis.TokenStream;
import org.codehaus.groovy.grails.validation.routines.UrlValidator;

class TestController {

    def fetchService
    def nameEntityService
    def tokenizeService
    def extractService

    def tokensFromAnalysis(analyzer, text) {
        def stream = analyzer.tokenStream("contents", new StringReader(text))
        def list = []
        while (true) {
            def token = stream.next()
            if (token) {
                list << token
            }  else {
                break
            }
        }
        return list
    }
    
    def index = {
        def url = params["blogUrl"]
        def result = fetchService.fetch(url)
        [url: url,  result: result]
    }

    def parseFeed = {
    	def url = params['link']
    	def type = params['type'] || 'rss'
    	def feeds = fetchService.fetchFeeds(url)
    	
    	def analyzer = new ThaiAnalyzer()    	
    	def result = feeds.entries.collect() { entry ->
    	    def content = FeedUtils.getContent(entry).replaceAll("<[^<]+>", '')
    	    def t1 = System.currentTimeMillis()
    	    def nameEntities = nameEntityService.extract(content)
    	    def t2 = System.currentTimeMillis()

    	    def t3 = System.currentTimeMillis()
    	    def tokens = tokensFromAnalysis(analyzer, content)
    	    def t4 = System.currentTimeMillis()  
    	    
    	    def mod = entry.getModule('http://rssnamespace.org/feedburner/ext/1.0')
    	    [
                content: content,
    	        tokens: tokens,
    	        title: entry.title,
    	        link: mod == null ? entry.link : mod.origLink,
    	        nameEntities: nameEntities,
    	        timeNameEntity: t2-t1,
    	        timeLuceneAnalysis: t4-t3
            ]
    	}
    	
    	[url: url, feeds: result]
    }

    def parseEntry = {
        def url = params['link']
        def html = fetchService.parseHtml(url).html
        extractService.extractEntry(html)
    }
    
}
